# getClusters.R
# created 2014 January 30

getClusters <- function (modelObject) {
  # Given an object of class "lm" or "ivreg", return the vector that indicates the
  # cluster for each person in the regression.  [2015 01 30]

  if ('ivreg' %in% class(modelObject)) { 
    modelRHS <- as.character(modelObject$formula[3])
  } else if ('lm' %in% class(modelObject)) {
    modelRHS <- as.character(modelObject$terms)[3]
  }
  
  if (grepl('factor(yearYoungNorm)', modelRHS, fixed = TRUE)) { 
    droplevels(modelObject$model$stateYoung:modelObject$model$'factor(yearYoungNorm)')
  } else {
    droplevels(modelObject$model$stateYoung:factor(modelObject$model$yearYoungNorm))
  }
}


